/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.entity.Dispatch;
import com.jeeplus.modules.tms.mapper.DispatchMapper;
import com.jeeplus.modules.tms.entity.DispatchAppend;
import com.jeeplus.modules.tms.mapper.DispatchAppendMapper;
import com.jeeplus.modules.tms.entity.DispatchDetail;
import com.jeeplus.modules.tms.mapper.DispatchDetailMapper;

/**
 * 调度单Service
 * @author dhw
 * @version 2017-11-29
 */
@Service
@Transactional(readOnly = true)
public class DispatchService extends CrudService<DispatchMapper, Dispatch> {

	@Autowired
	private DispatchAppendMapper dispatchAppendMapper;
	@Autowired
	private DispatchDetailMapper dispatchDetailMapper;
	
	public Dispatch get(String id) {
		Dispatch dispatch = super.get(id);
		dispatch.setDispatchAppendList(dispatchAppendMapper.findList(new DispatchAppend(dispatch)));
		dispatch.setDispatchDetailList(dispatchDetailMapper.findList(new DispatchDetail(dispatch)));
		return dispatch;
	}
	
	public List<Dispatch> findList(Dispatch dispatch) {
		return super.findList(dispatch);
	}
	
	public Page<Dispatch> findPage(Page<Dispatch> page, Dispatch dispatch) {
		return super.findPage(page, dispatch);
	}
	
	@Transactional(readOnly = false)
	public void save(Dispatch dispatch) {
		super.save(dispatch);
		for (DispatchAppend dispatchAppend : dispatch.getDispatchAppendList()){
			if (dispatchAppend.getId() == null){
				continue;
			}
			if (DispatchAppend.DEL_FLAG_NORMAL.equals(dispatchAppend.getDelFlag())){
				if (StringUtils.isBlank(dispatchAppend.getId())){
					dispatchAppend.setTmsDispatch(dispatch);
					dispatchAppend.preInsert();
					dispatchAppendMapper.insert(dispatchAppend);
				}else{
					dispatchAppend.preUpdate();
					dispatchAppendMapper.update(dispatchAppend);
				}
			}else{
				dispatchAppendMapper.delete(dispatchAppend);
			}
		}
		for (DispatchDetail dispatchDetail : dispatch.getDispatchDetailList()){
			if (dispatchDetail.getId() == null){
				continue;
			}
			if (DispatchDetail.DEL_FLAG_NORMAL.equals(dispatchDetail.getDelFlag())){
				if (StringUtils.isBlank(dispatchDetail.getId())){
					dispatchDetail.setTmsDispatch(dispatch);
					dispatchDetail.preInsert();
					dispatchDetailMapper.insert(dispatchDetail);
				}else{
					dispatchDetail.preUpdate();
					dispatchDetailMapper.update(dispatchDetail);
				}
			}else{
				dispatchDetailMapper.delete(dispatchDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Dispatch dispatch) {
		super.delete(dispatch);
		dispatchAppendMapper.delete(new DispatchAppend(dispatch));
		dispatchDetailMapper.delete(new DispatchDetail(dispatch));
	}
	
}